package hxy.dragon.location_record;

import android.os.Bundle;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.google.android.material.snackbar.Snackbar;
import com.kongzue.dialogx.dialogs.BottomDialog;
import com.kongzue.dialogx.interfaces.OnBindView;
import com.tencent.map.geolocation.TencentLocation;
import com.tencent.map.geolocation.TencentLocationListener;
import com.tencent.map.geolocation.TencentLocationManager;
import com.tencent.tencentmap.mapsdk.maps.CameraUpdateFactory;
import com.tencent.tencentmap.mapsdk.maps.MapView;
import com.tencent.tencentmap.mapsdk.maps.TencentMap;
import com.tencent.tencentmap.mapsdk.maps.model.BitmapDescriptorFactory;
import com.tencent.tencentmap.mapsdk.maps.model.LatLng;
import com.tencent.tencentmap.mapsdk.maps.model.Marker;
import com.tencent.tencentmap.mapsdk.maps.model.MarkerOptions;

import hxy.dragon.location_record.databinding.FragmentSecondBinding;
import hxy.dragon.location_record.tencent.LocationModel;
import hxy.dragon.location_record.tencent.NetworkService;

public class SecondFragment extends Fragment implements TencentLocationListener {

    private TencentMap mTencentMap;
    protected MapView mapV = null;
    private boolean isOnTop = false;

    private FragmentSecondBinding binding;

    private TencentLocationManager mLocationManager;
    Marker marker;

    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {

        binding = FragmentSecondBinding.inflate(inflater, container, false);
        return binding.getRoot();

    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        MapView mapView = (MapView) getView().findViewById(R.id.mapview);
        //获取地图实例
        mTencentMap = mapView.getMap();
//第一次渲染成功的回调
        mTencentMap.setOnMapLoadedCallback(new TencentMap.OnMapLoadedCallback() {
            public void onMapLoaded() {
                //地图正常显示
            }
        });

//设置当前位置可见
        mTencentMap.setMyLocationEnabled(true);
        mTencentMap.setMapCenterAndScale(0.5F, 0.5F, 14.5F);


        // 配置文档  https://lbs.qq.com/mobile/androidLocationSDK/androidGeoGuide/androidGeoSingle
        mLocationManager = TencentLocationManager.getInstance(getContext());
        mLocationManager.requestSingleFreshLocation(null, this, Looper.getMainLooper());

        Button button = (Button) getView().findViewById(R.id.location_now);
        button.setOnClickListener(e -> {
            mLocationManager.requestSingleFreshLocation(null, this, Looper.getMainLooper());
        });


        binding.fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 设置更新时间
//                long interval = intervals[i++ % intervals.length];
//
//                String timeValue = Time.desplay(interval);
//
//                Intent intent = new Intent(Constant.BroadCast.INTERVAL);
//                intent.putExtra(Constant.INTERVAL, interval);
//                sendBroadcast(intent);
//                BottomDialog.show("标题", "这里是对话框内容。");
                BottomDialog.show("标题1", "这里是对话框内容。\n底部对话框也支持自定义布局扩展使用方式。",
                        new OnBindView<BottomDialog>(R.layout.activity_about) {
                            @Override
                            public void onBind(BottomDialog dialog, View v) {
                                //v.findViewById...
                            }
                        });

                Snackbar.make(view, "联系作者微信 aohanhongyi" , Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public void onDestroyView() {
        if (mapV != null) {
            this.mapV.onDestroy();
        }
        super.onDestroyView();
        binding = null;
    }


    @Override
    public void onLocationChanged(TencentLocation tencentLocation, int i, String s) {
        if (marker != null) {
            marker.remove();
        }
        LatLng latLng = new LatLng(tencentLocation.getLatitude(), tencentLocation.getLongitude());
        mTencentMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        // 添加一个地图中心点标注
        MarkerOptions markerOptions = new MarkerOptions(mTencentMap.getCameraPosition().target);
        marker = mTencentMap.addMarker(markerOptions);
        marker.setTitle(tencentLocation.getAddress());
        marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));

//        NetworkService.postToServer(LocationModel.buildLocationModel(tencentLocation, userId));

    }

    @Override
    public void onStatusUpdate(String s, int i, String s1) {

    }
}